package com.zengqingfa.rocketmq.demo;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.autoconfigure.RocketMQProperties;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.rocketmq.spring.support.RocketMQHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @fileName: HelloController
 * @author: zengqf3
 * @date: 2021-4-2 22:51
 * @description:
 */
@RestController
@RequestMapping("/hello")
@Slf4j
public class HelloController {

    @Autowired
    RocketMQProperties rocketMQProperties;

    @Value("${rocketmq.topic.demo01}")
    private String topic;

    @Value("${rocketmq.tag.demo01}")
    private String tag;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @RequestMapping("/sendMsg")
    public String sendMsg() {
        List<Message> msgs = new ArrayList<>();
        for (int i = 0; i < 1; i++) {
            Message<String> message = MessageBuilder.withPayload("rocketMQTemplate transactional message " + i).build();
            msgs.add(message);
        }
        // 同步发送
        SendResult sendResult = rocketMQTemplate.syncSend(topic + ":" + tag, msgs);
        log.info("sendResult:{}", JSON.toJSONString(sendResult));
        return "SUCCESS";
    }

    @RequestMapping("/sendTxMsg")
    public String sendTxMsg() {
        //封装消息
        Message<String> message = MessageBuilder.withPayload("rocketMQTemplate transactional message ")
                .setHeader(RocketMQHeaders.TRANSACTION_ID, "KEY_HEADER").build();
        /**
         * 2.0.2中的事务消息方法:sendMessageInTransaction(final String txProducerGroup, final String destination,
         *                                              final Message<?> message, final Object arg)
         * 2.1.0中的事务消息方法:sendMessageInTransaction(final String destination,
         *                                               final Message<?> message, final Object arg)
         *  去除了txProducerGroup, 同时发现@RocketMQTransactionListener注解中也同样移除了txProducerGroup
         *
         *  在rocketmq-spring-boot-starter < 2.1.0以前的项目中，可以用多个@RocketMQTransactionListener来监听
         *  不同的txProducerGroup来发送不同类型的事务消息到topic， 但是现在在一个项目中，如果你在一个project中
         *  写了多个@RocketMQTransactionListener，项目将不能启动，启动会报
         *  java.lang.IllegalStateException: rocketMQTemplate already exists RocketMQLocalTransactionListener
         */
        // 发送事务消息
        SendResult sendResult = rocketMQTemplate.sendMessageInTransaction(topic + ":" + tag, message, null);
        log.info("sendResult:{}", JSON.toJSONString(sendResult));
        return "SUCCESS";
    }
}
